package com.cs.controller;

import com.cs.pojo.Role;
import com.cs.pojo.User;
import com.cs.resultVO.ResultVO;
import com.cs.resultVO.ResultVO2;
import com.cs.resultVO.ResultVOUserIndex;
import com.cs.service.RoleService;
import com.cs.service.UserService;
import com.cs.util.UserUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("user")
@ResponseBody
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private RoleService roleService;
    @RequestMapping(value = "list.do")
    public ResultVOUserIndex<User> queryByPage(String username, Integer pageNum, Integer pageSize){
        if(pageNum==null||pageNum<=0){
            pageNum=1;
        }
        if(pageSize==null||pageSize<=0){
            pageSize=5;
        }
        PageInfo<User> userPageInfo = userService.queryAll(username, pageNum, pageSize);
        List<Role> roles = roleService.queryAll();
        System.out.println(userPageInfo);
        return new ResultVOUserIndex<>(userPageInfo,roles);
    }
    @RequestMapping("edit.do")
    public ResultVO2 queryById(int uId){
        User user = userService.queryById(uId);
        List<Role> roles = roleService.queryAll();
        List<User> users = userService.queryAll2();
        return new ResultVO2(roles,user,users);
    }

    @RequestMapping("update.do")
    public ResultVO<User> update(User user){
        int update = userService.update(user);
        if (update==1){
            return new ResultVO<>();
        }else {
            return new ResultVO<>(500,"服务器内部错误");
        }
    }


    @RequestMapping("deleteone.do")
    public ResultVO<User> deleteone(@RequestParam(value = "uId",required = false) int uId){
        int i = userService.deleteOne(uId);
        if(i==1){
            return new ResultVO<>();
        }else {
            return new ResultVO<>(500,"服务器内部错误");
        }
    }

    @RequestMapping("updatePassword.do")
    public ResultVO<User> updatePassword(HttpServletRequest request, String newpass){
        int i = userService.updatePassword(UserUtil.getUserName(request.getSession()), newpass);
        if (i>0){
            return new ResultVO<>();
        }else {
            return new ResultVO<>(500,"服务器内部错误");
        }
    }

    @RequestMapping("getroleinfo.do")
    public ResultVO<Role> getRoleInfo(){
        List<Role> roles = roleService.queryAll();
        return new ResultVO<>(roles);
    }

    @RequestMapping("add.do")
    public ResultVO<User> add(User user){
        int add = userService.add(user);
        if (add>0){
            return new ResultVO<>();
        }else {
            return new ResultVO<>(500,"服务器内部错误");
        }
    }

    @RequestMapping("getuserlist.do")
    public ResultVO<User> getUserList(){
        List<User> users = userService.queryAll2();
        return new ResultVO<>(users);
    }

}
